﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Repository;
using Model.Domain_Model;
using Model.Query_Model;
using Interfaces.General_Interfaces;
using Views.Add_Views;
using Views.Status_Views;
using Views.Update_Views;
using Presenters;
using Extenions.DataGridViewSection;
using Views.Delete_Views;

namespace Views.General_Views
{
    public partial class RoadsView : UserControl, IRoadsView
    {
        RoadsViewPresenter presenter = null;

        public RoadsView()
        {
            InitializeComponent();
            presenter = new RoadsViewPresenter(this);
        }

        protected override void OnLoad(EventArgs e)
        {
            presenter.GetAllRoads();
            base.OnLoad(e);
        }

        T Interfaces.Inherited_Interfaces.IView.ReturnView<T>()
        {
            throw new NotImplementedException();
        }

        void Interfaces.Inherited_Interfaces.IError.ShowError()
        {
            presenter.ShowError(new ErrorView());
        }

        void Interfaces.Inherited_Interfaces.ISuccess.ShowSuccess()
        {
            throw new NotImplementedException();
        }

        void IParamable.SetParams(params object[] param)
        {
            throw new NotImplementedException();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            presenter.RoadAddDisplay(new RoadNewView());
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            presenter.GetAllRoads();
        }

        void Interfaces.Inherited_Interfaces.IGrid.HideColumn()
        {
            throw new NotImplementedException();
        }

        void Interfaces.Inherited_Interfaces.IGrid.MappingColumnName()
        {
            throw new NotImplementedException();
        }

        void Interfaces.Inherited_Interfaces.IGrid.ResizeColumn()
        {
            throw new NotImplementedException();
        }

        IList<Model.Query_Model.RoadOutput> IRoadsView.roadList
        {
            get
            {
                return (List<RoadOutput>)gridRoadList.DataSource;
            }
            set
            {
                IList<RoadOutput> roadList = value as List<RoadOutput>;
                int index=0;
                DefineColumn();
                foreach (RoadOutput road in roadList)
                {
                    gridRoadList.Rows.Add();
                    gridRoadList.Rows[index].Cells[0].Value = road.idRoad.ToString();
                    gridRoadList.Rows[index].Cells[1].Value = road.client.nameCorporation;
                    gridRoadList.Rows[index].Cells[2].Value = road.fromCity;
                    gridRoadList.Rows[index].Cells[3].Value = road.fromCountry;
                    gridRoadList.Rows[index].Cells[4].Value = road.toCity;
                    gridRoadList.Rows[index].Cells[5].Value = road.toCountry;
                    gridRoadList.Rows[index].Cells[7].Value = road.valuation.value.ToString();
                    gridRoadList.Rows[index].Cells[8].Value = road.valuation.currency.shortcut;
                    index++;
                }
            }
        }

        private void DefineColumn()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("idRoad", "Id");
            dict.Add("nameCorporation", "Firma");
            dict.Add("fromCity", "Miasto (start)");
            dict.Add("fromCountry", "Państwo (start)");
            dict.Add("toCity", "Miasto (stop)");
            dict.Add("toCountry", "Państwo (stop)");
            dict.Add("typeRoad", "Typ");
            dict.Add("value", "Wartość");
            dict.Add("shortcut", "Waluta");
            gridRoadList.DefineColumn(dict);
        }

        private void gridRoadList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            presenter.SelectRoad(Convert.ToInt32(gridRoadList.Rows[e.RowIndex].Cells[0].Value));
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            presenter.RoadDeleteDisplay(new RoadDeleteView());
        }

        private void edytujWycenęToolStripMenuItem_Click(object sender, EventArgs e)
        {
            presenter.ValuationUpdateDisplay(new ValuationUpdateView());
        }

        private void edytujTraseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            presenter.ValuationUpdateDisplay(new ValuationUpdateView());
        }

        private void edytujZaznaczoneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            presenter.RoadUpdateDisplay(new RoadUpdateView());
        }

        private void nowyDokumentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            presenter.DocumentAdd(new DocumentAddView());
        }
    }
}
